package com.mh.main;

import cn.hutool.core.date.DateTime;
import com.mh.mh.entity.GuaEntity;
import com.mh.mh.entity.GuaResutEntity;
import com.mh.mh.tool.BaGuaUtil;
import com.mh.mh.util.BihuaUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author Europe
 */
@Slf4j
@Component
public class MeiHuaStart {

    @Autowired
    BihuaUtil bihuaUtil;
    @Autowired
    BaGuaUtil baGuaUtil;

    /**
     * 计算爻用的先天数
      */
    public final int JING = 8;

    /**
     * 计算动爻用的数字
     */
    public final int DONG = 6;

    @PostConstruct
    public void startLife(){
        log.info("开始计算.........");
        DateTime dateTime = new DateTime();
        int seconds = dateTime.getSeconds();

        int num1 = bihuaUtil.getStrokeCount('阳');
        int num2 = bihuaUtil.getStrokeCount('车');
        int dongNum =  num1 + num2 + seconds;
//        int num1 = 6;
//        int num2 = 12;
//        int dongNum =  38;
        log.info("num1:"+num1+" | num2:"+num2+" | dongNum:"+dongNum);

        int yu1;
        int yu2;
        int dongYu;

        if(num1 <= JING){
            yu1 = num1;
        }else {
            yu1 = num1 % JING;
            if(yu1 == 0){
                yu1 = 8;
            }
        }

        if(num2 <= JING){
            yu2 = num2;
        }else {
            yu2 = num2 % JING;
            if(yu2 == 0){
                yu2 = 8;
            }
        }

        if(dongNum <= DONG){
            dongYu = dongNum;
        }else {
            dongYu = dongNum % DONG;
            if(dongYu == 0){
                dongYu = 6;
            }
        }

        String gua1 = baGuaUtil.tuiYan(yu1);
        String gua2 = baGuaUtil.tuiYan(yu2);
//        String gua1 = "000";
//        String gua2 = "001";
//        dongYu = 4;
        log.info("gua1 : "+gua1+" | gua2 : "+gua2+" | 动爻:"+dongYu);

        // 主卦
        GuaEntity zhuGuaEntity = new GuaEntity(gua1, gua2);
        baGuaUtil.setWuXing(zhuGuaEntity);
        // 互卦
        GuaEntity huGuaEntity = new GuaEntity(gua1.substring(1,3) + gua2.charAt(0),
                                    gua1.substring(2) + gua2.substring(0,2));
        baGuaUtil.setWuXing(huGuaEntity);
        // 变卦
        GuaEntity bianGuaEntity = new GuaEntity();
        if((dongYu - 3) > 0){
            String bian = baGuaUtil.getBianGua(dongYu - 3, gua1);
            bianGuaEntity.setUpYao(bian);
            bianGuaEntity.setDownYao(gua2);

            zhuGuaEntity.setTiyong("upYao");
            huGuaEntity.setTiyong("upYao");
            bianGuaEntity.setTiyong("upYao");
        }else{
            String bian = baGuaUtil.getBianGua(dongYu, gua2);
            bianGuaEntity.setUpYao(gua1);
            bianGuaEntity.setDownYao(bian);

            zhuGuaEntity.setTiyong("downYao");
            huGuaEntity.setTiyong("downYao");
            bianGuaEntity.setTiyong("downYao");
        }
        baGuaUtil.setWuXing(bianGuaEntity);

        log.info("主卦 ========> "+zhuGuaEntity.toString());
        log.info("互卦 ========> "+huGuaEntity.toString());
        log.info("变卦 ========> "+bianGuaEntity.toString());

        GuaResutEntity guaResutEntity = baGuaUtil.duanGua(zhuGuaEntity, huGuaEntity, bianGuaEntity);
        log.info(guaResutEntity.toString());

    }

    public static void main(String[] args) {

        String str = "123";
        String str2 = "456";
        System.out.println(str.charAt(0)+"");
        System.out.println(str.charAt(1)+"");
        System.out.println(str.charAt(2)+"");
        System.out.println(str2.substring(0,2));



    }





}
